//
//  RankView.m
//  CYWFrame
//
//  Created by Macx on 2017/8/29.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "RankView.h"
#import "User.h"
#import "HomeServer.h"

@implementation RankView {
    NSString    *_url;
    NSString    *_name;
    NSInteger   _type;
}

- (instancetype)initWithFrame:(CGRect)frame type:(NSInteger)type {
    self = [super initWithFrame:frame];
    if (self) {
        
        _type = type;
        if (type == 1) {
            _url = APIRankContributions;
            _name = @"送礼";
        }
        else if (type == 2) {
            _url = APIRankStar;
            _name = @"收礼";
        }
        else if (type == 3) {
            _url = APIRankFollowers;
            _name = @"粉丝";
        }
        
        [self buildUI];
        
        // 星光排行
        if (type == 1 || type == 2) {
            [self GETRankStars2];
            [self GETRankStars3];
        }
        [self GETRankStars1];
        
    }
    return self;
}

- (void)buildUI {
    
    NSArray *wayArr = @[@"日榜", @"月榜", @"总榜"];
    CGFloat Y = 0.0;
    if (_type == 1 || _type == 2) {
        
        for (int i = 0; i < wayArr.count; i++) {
            UIButton *wayBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [self addSubview:wayBtn];
            [wayBtn setTitle:[NSString stringWithFormat:@"    %@    ", wayArr[i]] forState:UIControlStateNormal];
            wayBtn.titleLabel.font = kUHSystemFontWithSize(15);
            CGSize bsize = [LZCTools textSizeWithText:wayBtn.titleLabel.text font:kUHSystemFontWithSize(15)];
            CGFloat x = (SW(3)-bsize.width)/3;
            wayBtn.frame = CGRectMake(x+SW(3)*i, 10, bsize.width, SW(16));
            [wayBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [wayBtn addTarget:self action:@selector(clickWay:) forControlEvents:UIControlEventTouchUpInside];
            wayBtn.tag = 11+i;
            
            Y = wayBtn.y+bsize.height+10;
            
            UIView *pinkV = [[UIView alloc] initWithFrame:CGRectMake(wayBtn.x, Y-2, wayBtn.width, 2)];
            [self addSubview:pinkV];
            pinkV.tag = 21+i;
            if (i==0) {
                pinkV.backgroundColor = UIMainColorRGB;
                [wayBtn setTitleColor:UIMainColorRGB forState:UIControlStateNormal];
            }
        }
    }
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, Y, SCREEN_WIDTH, 10)];
    [self addSubview:line];
    line.backgroundColor = UISeparatedColorRGB;
    
    _tableView1 = [[UITableView alloc] initWithFrame:CGRectMake(0, line.y+10, SCREEN_WIDTH, self.height-line.y-10) style:UITableViewStylePlain];
    [self addSubview:_tableView1];
    _tableView1.delegate = self;
    _tableView1.dataSource = self;
    //    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView1.tableFooterView = [[UIView alloc]init];
    [_tableView1 registerNib:[UINib nibWithNibName:@"RankTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    _tableView1.backgroundColor = UISeparatedColorRGB;
    
    if (_type == 1 || _type == 2) {
        _tableView2 = [[UITableView alloc] initWithFrame:CGRectMake(0, line.y+10, SCREEN_WIDTH, self.height-line.y-10) style:UITableViewStylePlain];
        [self addSubview:_tableView2];
        _tableView2.delegate = self;
        _tableView2.dataSource = self;
        //    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView2.tableFooterView = [[UIView alloc]init];
        [_tableView2 registerNib:[UINib nibWithNibName:@"RankTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        _tableView2.hidden = YES;
        _tableView2.backgroundColor = UISeparatedColorRGB;
        
        _tableView3 = [[UITableView alloc] initWithFrame:CGRectMake(0, line.y+10, SCREEN_WIDTH, self.height-line.y-10) style:UITableViewStylePlain];
        [self addSubview:_tableView3];
        _tableView3.delegate = self;
        _tableView3.dataSource = self;
        //    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView3.tableFooterView = [[UIView alloc]init];
        [_tableView3 registerNib:[UINib nibWithNibName:@"RankTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        _tableView3.hidden = 30;
        _tableView3.backgroundColor = UISeparatedColorRGB;
    }
    
}


#pragma mark ==========UITableView==========
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    _cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    _cell.rank.text = [NSString stringWithFormat:@"%ld", indexPath.row];
    
    if ([_tableView1 isEqual:tableView]) {
        User *model = self.dayArr[indexPath.row];
        _cell.model = model;
        
        _cell.optionsBtn.tag = indexPath.row;
        [_cell.optionsBtn addTarget:self action:@selector(fansAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    else if ([_tableView2 isEqual:tableView]) {
        User *model = self.weekArr[indexPath.row];
        _cell.model = model;
        
        _cell.optionsBtn.tag = indexPath.row;
        [_cell.optionsBtn addTarget:self action:@selector(fansAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    else {
        User *model = self.allArr[indexPath.row];
        _cell.model = model;
        
        _cell.optionsBtn.tag = indexPath.row;
        [_cell.optionsBtn addTarget:self action:@selector(fansAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    
    
    return _cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([_tableView1 isEqual:tableView]) {
        return self.dayArr.count;
    }
    else if ([_tableView2 isEqual:tableView]) {
        return self.weekArr.count;
    }
    else
        return self.allArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return SW(7)+20;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"点击tableView.cell = %ld", indexPath.row);
}

#pragma mark ==========响应方法==========
- (void)clickWay:(UIButton *)sender {
    
    UIButton *btn1  = [self viewWithTag:11];
    UIView   *view1 = [self viewWithTag:21];
    UIButton *btn2  = [self viewWithTag:12];
    UIView   *view2 = [self viewWithTag:22];
    UIButton *btn3  = [self viewWithTag:13];
    UIView   *view3 = [self viewWithTag:23];
    
    if (sender.tag == 11) {
        view1.backgroundColor = UIMainColorRGB;
        view2.backgroundColor = kClearColor;
        view3.backgroundColor = kClearColor;
        [btn1 setTitleColor:UIMainColorRGB forState:UIControlStateNormal];
        [btn2 setTitleColor:kGrayColor forState:UIControlStateNormal];
        [btn3 setTitleColor:kGrayColor forState:UIControlStateNormal];
        _tableView1.hidden = NO;
        _tableView2.hidden = YES;
        _tableView3.hidden = YES;
    }
    else if (sender.tag == 12) {
        view1.backgroundColor = kClearColor;
        view2.backgroundColor = UIMainColorRGB;
        view3.backgroundColor = kClearColor;
        [btn1 setTitleColor:kGrayColor forState:UIControlStateNormal];
        [btn2 setTitleColor:UIMainColorRGB forState:UIControlStateNormal];
        [btn3 setTitleColor:kGrayColor forState:UIControlStateNormal];
        _tableView1.hidden = YES;
        _tableView2.hidden = NO;
        _tableView3.hidden = YES;
    }
    else {
        view1.backgroundColor = kClearColor;
        view2.backgroundColor = kClearColor;
        view3.backgroundColor = UIMainColorRGB;
        [btn1 setTitleColor:kGrayColor forState:UIControlStateNormal];
        [btn2 setTitleColor:kGrayColor forState:UIControlStateNormal];
        [btn3 setTitleColor:UIMainColorRGB forState:UIControlStateNormal];
        _tableView1.hidden = YES;
        _tableView2.hidden = YES;
        _tableView3.hidden = NO;
    }
    
}

// 星光排行
- (void)GETRankStars1 {
    
     // GET /lives/ranklists/stars 获取直播星光值排行榜 type 排行榜类型 day(日) week(周) all(总)，默认为day limit 限制数量 默认10
//#define APIRankStar                       [APIRank stringByAppendingString:@"/stars"]
    
    NSDictionary *param;
    if (_type == 3) {
        param = @{@"limit":@"30"};
        _name = @"粉丝榜";
    }
    else {
        param = @{@"type":@"day",
                  @"limit":@"30"};
        _name = [NSString stringWithFormat:@"每日%@", _name];
    }
    
    [[NetworkTool sharedTool] requestWithName:_name URLString:_url parameters:param method:@"GET" callBack:^(id responseObject) {
       
        for (NSDictionary *dic in responseObject[@"data"]) {
            User *model = [[User alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dayArr addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [_tableView1 reloadData];
        });

    }];
    
}

- (void)GETRankStars2 {
    NSDictionary *param = @{@"type":@"week",
                            @"limit":@"30"};
    [[NetworkTool sharedTool] requestWithName:[NSString stringWithFormat:@"每周%@", _name] URLString:_url parameters:param method:@"GET" callBack:^(id responseObject) {
        for (NSDictionary *dic in responseObject[@"data"]) {
            User *model = [[User alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.weekArr addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [_tableView2 reloadData];
        });
    }];
}

- (void)GETRankStars3 {
    NSDictionary *param = @{@"type":@"all",
                            @"limit":@"30"};
    [[NetworkTool sharedTool] requestWithName:[NSString stringWithFormat:@"全部%@", _name] URLString:_url parameters:param method:@"GET" callBack:^(id responseObject) {
        for (NSDictionary *dic in responseObject[@"data"]) {
            User *model = [[User alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.allArr addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [_tableView3 reloadData];
        });
    }];
}


/**
 添加关注 or 取消关注
 */
- (void)fansAction:(UIButton *)sender {
    
    User *model;
    if (_tableView1.isHidden == NO) {
        model = self.dayArr[sender.tag];
    }
    else if (_tableView2.isHidden == NO) {
        model = self.weekArr[sender.tag];
    }
    else {
        model = self.allArr[sender.tag];
    }
    
    NSString *toId = [model valueForKey:@"user_id"];
    
    [HomeServer focus:sender toId:toId];
}


// 懒加载
- (NSMutableArray *)dayArr {
    if (_dayArr == nil) {
        _dayArr = [[NSMutableArray alloc] init];
    }
    return _dayArr;
}
- (NSMutableArray *)weekArr {
    if (_weekArr == nil) {
        _weekArr = [[NSMutableArray alloc] init];
    }
    return _weekArr;
}
- (NSMutableArray *)allArr {
    if (_allArr == nil) {
        _allArr = [[NSMutableArray alloc] init];
    }
    return _allArr;
}

@end
